<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* cache for request triggers piles
* @author J.Ducastel <jducastel@jducastel.fr>
* @version
*/
class wakTriggersPilesCache extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	* /
	function service_version() {
		$this->__construct();
	}/**/

	/**
	* "real" constructor
	* @access
	* /
	function __construct() {
		parent::__construct();
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* stores the triggers pile for http request into cache
	* @param waktriggerspile &$pile
	* @param httpRequestBean &$request
	* @access public
	* @return bool
	*/
	function storePileForRequest(&$pile,&$request) {
		// checking params
		if (wakapi::haswrongtype($pile,'waktriggerspile')
		or wakapi::haswrongtype($request,'httprequestbean'))
			return false;
		// getting request id
		$id=$this->getRequestId($request); //echo $id;
		// serializing pile
		$pileStorable=serialize($pile); //echo $pileStorable;
		// create folder if necessary
		$this->createFolderIfNecessary();
		// getting file node
		if (!wakapi::needclass('filenode'))
			return false;
		$path=$this->cfg['path'];
		$node=&new filenode($path.$id.'.txt');
		$node->erase(0);
		return $node->write($pileStorable);
	}/**/

	/**
	* retrieve triggers pile from cache for the request
	* @param httpRequestBean
	* @access public
	* @return waktriggerspile
	*/
	function & getPileForRequest(&$request) {
		$false=false;
		// getting request id
		$id=$this->getRequestId($request); //echo $id;
		// getting classes definitions
		if (!wakapi::needclass('waktriggerspile')
		or !wakapi::needclass('waktrigger')
		or !wakapi::needclass('wakparameters'))
			return $false;
		// getting file node
		if (!wakapi::needclass('filenode'))
			return $false;
		$path=$this->cfg['path'];
		$node=&new filenode($path.$id.'.txt');
		if (!$node->exists())
			return $false;
		// reading node
		$serialized=$node->read();
		$pile=unserialize($serialized);
		return $pile;
	}/**/

	/**
	*
	* @param
	* @access public
	* @return
	* /
	function () {

	}/**/

	/**
	*
	* @param
	* @access public
	* @return
	* /
	function () {

	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $cfg=array(
		'path'=>'tmp/cache/triggerspiles/'
	);
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* returns an id for the request
	* @param httprequestbean &$request
	* @access private
	* @return string
	*/
	function getRequestId(&$request) {
		//return md5(serialize($request);
		$url=$request->get('url');
		$footprint=$request->get('method')
		.'.'.$url->getrelative();
		return str_replace('/','-',$footprint);
		return md5($footprint);
	}/**/

	/**
	*
	* @param
	* @access private
	* @return bool
	*/
	function createFolderIfNecessary() {
		wakapi::needclass('foldernode');
		$cacheNode=&new foldernode('tmp/cache');
		if (!$cacheNode->exists())
			$cacheNode->create();
		$pilesNode=&new foldernode('tmp/cache/triggerspiles');
		if (!$pilesNode->exists())
			$pilesNode->create();
		return true;
	}/**/

	/**
	*
	* @param
	* @access private
	* @return
	* /
	function () {

	}/**/
}
?>
